package hoang.hut.networkdemo.network;

import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpParams;

import android.content.Context;
import android.os.AsyncTask;

public class AsyncHttpBase extends AsyncTask<String, Integer, String>{

	public static final int STATUS_NETWORK_OK = 0;
	public static final int STATUS_NETWORK_OFF = 1;
	public static final int STATUS_NETWORK_ERROR = 2;
	public static final int STATUS_NETWORK_TIMEOUT = 3;
	public static final int STATUS_NETWORK_EXCEIPTION = 4;
	
	protected Context mContext;
	AsyncHttpResponseProcess process;
	protected HttpResponse response;
	protected List<NameValuePair> parameter;
	protected int statusCode;
	
	public AsyncHttpBase(Context context, AsyncHttpResponseProcess process, List<NameValuePair> params){
		this.mContext = context;
		this.process = process;
		this.parameter = params;
	}
	
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		process.before();
	}
	
	@Override
	protected String doInBackground(String... params) {
		if(Utils.isNetworkAvailable(mContext)){
			return request(params[0]);
		}else{
			statusCode = STATUS_NETWORK_OFF;
		}
		return null;
	}
	
	protected String request(String url){
		return null;
	}
	/**
	 * phuong thuc thuc hien khi request ket thuc
	 */
	@Override
	protected void onPostExecute(String result) {
		super.onPostExecute(result);
		
		process.after(statusCode, response);
	}
	/**
	 * phuong thuc tao http client 
	 * 
	 * @param params
	 * @return
	 */
	protected HttpClient creatHttpClient(HttpParams params){
		return new DefaultHttpClient(params);
	}
	
}
